package Thread;

/**
 * Created with IntelliJ IDEA.
 * Description: 线程安全学习代码示例2：内存可见性
 */
public class ThreadDemo16 {
    public static int isQuit = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                //Java编译器进行代码优化（在保证逻辑不变的情况）
                //可能会不再从主内存中读取数据
                //而是直接从工作内存中读取数据，导致t1死循环
                while(isQuit == 0) {
                    synchronized (this) {

                    }
                }
                System.out.println("循环结束，t1线程执行完毕");
            }
        };
        t1.start();
        Thread.sleep(2000);
        isQuit = 1;
        System.out.println("main线程执行完毕");
    }
}
